const moment = require('moment');
module.exports = app => {
  const {
    STRING,
    INTEGER,
    DATE
  } = app.Sequelize; //获取数据类型
  const CookBook = app.model.define('cookbook', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    }, //id 为cookbook 表内的 键 并且为 自增 整形数,
    name: {
      type: STRING,
      allowNull: false
    },
    desc: {
      type: STRING(1000),
      allowNull: true
    },
    cover: {
      type: STRING(1000),
      allowNull: true
    },
    state: {
      type: INTEGER,
      allowNull: true,
      defaultValue: 2 // 1：上架(未上架)  2：下架(已上架)
    },
    category_id: {
      type: INTEGER,
      allowNull: false
    },
    cook_type: { // 1：未通过审核  2：通过审核
      type: INTEGER,
      allowNull: true,
      defaultValue: 1
    },
    user_id: {
      type: INTEGER,
      allowNull: true
    },
    favorite_count: {
      type: INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    create_time: {
      type: DATE,
      allowNull: true,
      get() {
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    update_time: {
      type: DATE,
      allowNull: true,
      get() {
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    sort: {
      type: INTEGER,
      allowNull: true,
      defaultValue: 0
    }
  }, {
    // Model 对应的表名将与model名相同
    freezeTableName: true,
    //去除createAt updateAt
    timestamps: true,
    // 实例对应的表名
    tableName: 'cookbook',
    // 如果需要sequelize帮你维护createdAt,updatedAt和deletedAt必须先启用timestamps功能
    // 将createdAt对应到数据库的created_at字段
    createdAt: 'create_time',
    // 将updatedAt对应到数据库的updated_at字段
    updatedAt: 'update_time',
    //And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
    deletedAt: false, //'deleted_at',
    //删除数据时不删除数据，而是更新deleteAt字段 如果需要设置为true，则上面的deleteAt字段不能为false，也就是说必须启用
    paranoid: false
  })
  return CookBook
}